from template import *

import ldap
import time

class LdapPlugin(TemplateAuthenticationPlugin):
    """a plugin to use ldap bind to test authentication"""

    def __init__(self, *args, **kwargs):
        super(LdapPlugin, self).__init__(*args, **kwargs)
        self.server = str(self.config.server)
        self.organization = str(self.config.organization)
    
    def authenticate(self, username, password):
        response = False
        try:
            l = ldap.open(self.server)
            bind_string = "cn=%s,o=%s" % (username, self.organization)
            l.simple_bind_s(bind_string, "%s" % password)
            response = True
        except Exception, e:
            response = False
        else:
            l.unbind()
        return response
    
